<?php
namespace app\api\controller;
use think\Db;/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 虚拟订单控制器
 */
class  Membervrorder extends MobileMember
{
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
    }


    /**
     * @api {POST} api/Membervrorder/order_list 订单列表
     * @apiVersion 1.0.0
     * @apiGroup Membervrorder
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} page 当前页数
     * @apiParam {Int} state_type 订单状态 state_new待付款 state_pay待使用
     * @apiParam {String} order_key 订单编号
     * @apiParam {Int} per_page 每页数量
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.order_list  订单列表 （返回字段参考vrorder表）
     * @apiSuccess {Object[]} result.order_list.code_list 未兑换的兑换码列表 （返回字段参考vrordercode表）
     * @apiSuccess {Int} result.order_list.if_cancel  是否可取消 true是false否
     * @apiSuccess {Int} result.order_list.if_pay  是否可支付 true是false否
     * @apiSuccess {Int} result.order_list.if_refund  是否可退款 true是false否
     * @apiSuccess {Int} result.order_list.if_evaluation  是否可评价 true是false否
     * @apiSuccess {String} result.order_list.goods_image_url  商品图片完整地址
     * @apiSuccess {Int} result.order_list.if_refund_cancel  是否可全部退款 true是false否
     * @apiSuccess {Int} result.page_total  总页数
     * @apiSuccess {Boolean} result.hasmore  是否有更多 true是false否
     */
    public function order_list()
    {
        $vrorder_model = model('vrorder');

        $condition = array();
        $condition['buyer_id'] = $this->member_info['member_id'];
        if (preg_match('/^\d{10,20}$/', input('post.order_key'))) {
            $condition['order_sn'] = input('post.order_key');
        }
        elseif (input('post.order_key') != '') {
            $condition['goods_name'] = array('like', '%' . input('post.order_key') . '%');
        }
        if (input('post.state_type') != '') {
            $condition['order_state'] = str_replace(array('state_new', 'state_pay'), array(
                                                                                       ORDER_STATE_NEW, ORDER_STATE_PAY
                                                                                   ), input('post.state_type'));
        }
        $order_list = $vrorder_model->getVrorderList($condition, $this->pagesize, '*', 'order_id desc');
        //没有使用的兑换码列表
        $order_list = $vrorder_model->getCodeRefundList($order_list);
        foreach ($order_list as $key => $order) {
            //显示取消订单
            $order_list[$key]['if_cancel'] = $vrorder_model->getVrorderOperateState('buyer_cancel', $order);

            //显示支付
            $order_list[$key]['if_pay'] = $vrorder_model->getVrorderOperateState('payment', $order);
            
            //显示退款
            $order_list[$key]['if_refund'] = $vrorder_model->getVrorderOperateState('refund', $order);

            //显示评价
            $order_list[$key]['if_evaluation'] = $vrorder_model->getVrorderOperateState('evaluation', $order);

            $order_list[$key]['goods_image_url'] = goods_cthumb($order['goods_image'], 240);

        }

        $result= array_merge(array('order_list' => $order_list), mobile_page($vrorder_model->page_info));
        ds_json_encode(10000, '',$result);
    }
    /**
     * @api {POST} api/Membervrorder/order_info 订单详情
     * @apiVersion 1.0.0
     * @apiGroup Membervrorder
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} order_id 订单ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.order_info  订单列表 （返回字段参考vrorder表）
     * @apiSuccess {Object[]} result.order_info.code_list 未兑换的兑换码列表 （返回字段参考vrordercode表）
     * @apiSuccess {Int} result.order_info.if_cancel  是否可取消 true是false否
     * @apiSuccess {Int} result.order_info.if_resend  是否可发送兑换码 true是false否
     * @apiSuccess {Int} result.order_info.if_refund  是否可退款 true是false否
     * @apiSuccess {Int} result.order_info.if_evaluation  是否可评价 true是false否
     * @apiSuccess {String} result.order_info.goods_image_url  商品图片完整地址
     * @apiSuccess {Int} result.order_info.if_refund_cancel  是否可全部退款 true是false否
     */
    public function order_info()
    {
        $order_id = intval(input('param.order_id'));
        if ($order_id <= 0) {
            ds_json_encode(10001,'订单不存在');
        }
        $vrorder_model = model('vrorder');
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['buyer_id'] = $this->member_info['member_id'];
        $order_info = $vrorder_model->getVrorderInfo($condition);
        if (empty($order_info) || $order_info['delete_state'] == ORDER_DEL_STATE_DROP) {
            ds_json_encode(10001,'订单不存在');
        }
        $order_list = array();
        $order_list[$order_id] = $order_info;
        $order_list = $vrorder_model->getCodeRefundList($order_list); //没有使用的兑换码列表
        $order_info = $order_list[$order_id];
        //显示取消订单
        $order_info['if_cancel'] = $vrorder_model->getVrorderOperateState('buyer_cancel', $order_info);

        //显示评价
        $order_info['if_evaluation'] = $vrorder_model->getVrorderOperateState('evaluation', $order_info);

        //显示退款
        $order_info['if_refund'] = $vrorder_model->getVrorderOperateState('refund', $order_info);

        $order_info['goods_image_url'] = goods_cthumb($order_info['goods_image'], 240);


        $order_info['vr_indate'] = $order_info['vr_indate'] ? date('Y-m-d', $order_info['vr_indate']) : '';
        $order_info['add_time'] = date('Y-m-d', $order_info['add_time']);
        $order_info['payment_time'] = $order_info['payment_time'] ? date('Y-m-d', $order_info['payment_time']) : '';
        $order_info['finnshed_time'] = $order_info['finnshed_time'] ? date('Y-m-d', $order_info['finnshed_time']) : '';

        $order_info['if_resend'] = $order_info['order_state'] == ORDER_STATE_PAY ? true : false;
        //取兑换码列表
        $vr_code_list = $vrorder_model->getShowVrordercodeList(array('order_id' => $order_info['order_id']));
        $order_info['code_list'] = $vr_code_list ? $vr_code_list : array();

        ds_json_encode(10000, '',array('order_info' => $order_info));
    }


    /**
     * @api {POST} api/Membervrorder/order_cancel 取消订单
     * @apiVersion 1.0.0
     * @apiGroup Membervrorder
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} order_id 订单ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    public function order_cancel()
    {
        $vrorder_model = model('vrorder');
        $condition = array();
        $condition['order_id'] = intval(input('post.order_id'));
        $condition['buyer_id'] = $this->member_info['member_id'];
        $order_info = $vrorder_model->getVrorderInfo($condition);

        $if_allow = $vrorder_model->getVrorderOperateState('buyer_cancel', $order_info);
        if (!$if_allow) {
            ds_json_encode(10001, '无权操作');
        }

        $logic_vrorder = model('vrorder','logic');
        $result = $logic_vrorder->changeOrderStateCancel($order_info, 'buyer', '其它原因');

        if (!$result['code']) {
            ds_json_encode(10001, $result['msg']);
        }
        else {
            ds_json_encode(10000, '');
        }
    }

    /**
     * 发送兑换码到手机
     */
    public function resend()
    {
        if (!preg_match('/^[\d]{11}$/', input('post.buyer_phone'))) {
            ds_json_encode(10001,'请正确填写手机号');
        }
        $order_id = intval(input('post.order_id'));
        if ($order_id <= 0) {
            ds_json_encode(10001,'参数错误');
        }

        $vrorder_model = model('vrorder');

        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['buyer_id'] = $this->member_info['member_id'];
        $order_info = $vrorder_model->getVrorderInfo($condition);
        if (empty($order_info) && $order_info['order_state'] != ORDER_STATE_PAY) {
            ds_json_encode(10001,'订单信息发生错误');
        }
        if ($order_info['vr_send_times'] >= 5) {
            ds_json_encode(10001,'您发送的次数过多，无法发送');
        }

        //发送兑换码到手机
        $param = array(
            'order_id' => $order_id, 'buyer_id' => $this->member_info['member_id'],
            'buyer_phone' => input('post.buyer_phone'), 'goods_name' => $order_info['goods_name']
        );
        \mall\queue\QueueClient::push('sendVrCode', $param);
        $vrorder_model->editVrorder(array('vr_send_times' => Db::raw('vr_send_times+1')), array('order_id' => $order_id));
        ds_json_encode(10000, '');
    }
}